import GameUI from "./GameUI";

export default class BlockControl extends Laya.Script3D {
    private block: Laya.MeshSprite3D
    private static MAX_POS = 5
    public direct: number
    public static speed: number = 0.05
    onEnable() {
        this.block = this.owner as Laya.MeshSprite3D
        BlockControl.speed = 0.05 + GameUI.instance.blockNum / 500
        // 根据分数确定初始位置
        if (GameUI.instance.blockNum % 2 == 0) {
            this.block.transform.localPosition = new Laya.Vector3(BlockControl.MAX_POS, this.block.transform.localPositionY, this.block.transform.localPositionZ)
            this.direct = 1
        } else {
            this.block.transform.localPosition = new Laya.Vector3(this.block.transform.localPositionX, this.block.transform.localPositionY, -BlockControl.MAX_POS)
            this.direct = -1
        }
    }

    onUpdate() {
        if (GameUI.instance.blockNum % 2 == 0) {
            this.block.transform.translate(new Laya.Vector3(-this.direct * BlockControl.speed, 0, 0), true)
            if (Math.abs(this.block.transform.localPositionX) >= BlockControl.MAX_POS) {
                this.direct *= -1
            }
        }
        else {
            this.block.transform.translate(new Laya.Vector3(0, 0, -this.direct * BlockControl.speed), true)
            if (Math.abs(this.block.transform.localPositionZ) >= BlockControl.MAX_POS) {
                this.direct *= -1
            }
        }
    }
}